﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
using cczEdit.DramaDialog; 
using cczEdit.CommonInstructComposite;
using cczEdit.Data;
namespace cczEdit
{
    public partial class UCDrama : UserControl
    {
        private AddDramaDialog addDramaDialog;
        private AddCommonInstruct addcommonInstructDialog;
        private EditInformationDialog editInfoDialog;
        private EditHeroTouchedDialog editHeroDialog;
        private EditDramaDialog editDramaDialog;
        private EditDramaChangeAIDialog editDramaChangeAIDialog;
        private EditDramaSetEquipmentDialog editDramaSetEquipDialog;
        private EditDramaSetPlayerBattleDialog editDramaSetPlayerBattleDialog;
        private EditDramaSetAIBattleDialog editDramaSetAIBattleDialog;
        private EditDramaSetStateDialog editDramaSetStateDialog;
        private EditDramaSetExitUnitDialog editDramaSetExitUnitDialog;
        private EditDramaSetWinReward editDramaSetWinRewardDialog;
        private DramaConvetDialog dramaConvertDialog;
        static public List<Hero> heros;
        static public List<Sundry> sundries;
        static public  List<Equipment> weapons;
        static public List<Equipment> armors;
        static public List<Equipment> assists;
        static public List<Magic> magics;
        private PListRoot currentFile;
        public FileData dramaFileData;
        public UCDrama()
        {
            InitializeComponent();
            LoadHeroData();
            heros=heros.OrderBy(k => k.name).ToList();
            LoadItemData(Form1.cczDirector + "//ItemData.plist");
            LoadMagicData(Form1.cczDirector + "//MagicData.plist");
            addDramaDialog = new AddDramaDialog();
            addcommonInstructDialog = new AddCommonInstruct();
            editInfoDialog = new EditInformationDialog();
            editHeroDialog = new EditHeroTouchedDialog(heros);
            editDramaDialog = new EditDramaDialog();
            editDramaChangeAIDialog = new EditDramaChangeAIDialog();
            editDramaSetEquipDialog = new EditDramaSetEquipmentDialog();
            editDramaSetPlayerBattleDialog = new EditDramaSetPlayerBattleDialog();
            editDramaSetAIBattleDialog = new EditDramaSetAIBattleDialog();
            editDramaSetStateDialog = new EditDramaSetStateDialog();
            editDramaSetExitUnitDialog = new EditDramaSetExitUnitDialog();
            editDramaSetWinRewardDialog = new EditDramaSetWinReward();
            dramaConvertDialog = new DramaConvetDialog();
            dramaFileData = new FileData();
        }
        void LoadHeroData()
        {
            heros = new List<Hero>(500);
            PListRoot root = PListRoot.Load(Form1.cczDirector + "\\HeroData.plist");
            PListDict rootDic = (PListDict)root.Root;
            PListDict herosDic = (PListDict)rootDic["Heros"];
            foreach (KeyValuePair<String, IPListElement> a in herosDic)
            {
                PListDict heroDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)heroDic["Num"];
                PListString name = (PListString)heroDic["Name"];
                PListInteger numHead = (PListInteger)heroDic["NumHead"];
                PListInteger numMsgCrit = (PListInteger)heroDic["NumMsgCrit"];
                PListInteger numMsgDeath = (PListInteger)heroDic["NumMsgDeath"];
                PListInteger numRAnim = (PListInteger)heroDic["NumRAnim"];
                PListInteger numSAnim = (PListInteger)heroDic["NumSAnim"];
                PListInteger numTeam = (PListInteger)heroDic["NumTeam"];
                PListString dspt = (PListString)heroDic["Dspt"];
                PListInteger agile = (PListInteger)heroDic["Agile"];
                PListInteger command = (PListInteger)heroDic["Command"];
                PListInteger force = (PListInteger)heroDic["Force"];
                PListInteger intelligence = (PListInteger)heroDic["Intelligence"];
                PListInteger luck = (PListInteger)heroDic["Luck"];
                PListInteger HP = (PListInteger)heroDic["HP"];
                PListInteger MP = (PListInteger)heroDic["MP"];
                Hero hero = new Hero();
                hero.name = name.Value;
                hero.num = num.Value;
                hero.numHead = numHead.Value;
                hero.numMsgCrit = numMsgCrit.Value;
                hero.numMsgDeath = numMsgDeath.Value;
                hero.numRAnim = numRAnim.Value;
                hero.numSAnim = numSAnim.Value;
                hero.numTeam = numTeam.Value;
                hero.dspt = dspt.Value;
                hero.agile = agile.Value;
                hero.command = command.Value;
                hero.force = force.Value;
                hero.intelligence = intelligence.Value;
                hero.luck = luck.Value;
                hero.hp = HP.Value;
                hero.mp = MP.Value;
                heros.Add(hero);

            }


        }
        private void LoadItemData(String fileName_)
        {
            sundries = new List<Sundry>();
            weapons = new List<Equipment>();
            armors = new List<Equipment>();
            assists = new List<Equipment>();
            PListRoot root = PListRoot.Load(fileName_);
            PListDict rootDic = (PListDict)root.Root;
            PListDict weaponsDic = (PListDict)rootDic["Weapon"];
            PListDict armorsDic = (PListDict)rootDic["Armor"];
            PListDict assistsDic = (PListDict)rootDic["Assist"];
            PListDict SundrisDic = (PListDict)rootDic["Sundry"];
            foreach (KeyValuePair<String, IPListElement> a in weaponsDic)
            {
                PListDict weaponDic = (PListDict)a.Value;
                PListString name = (PListString)weaponDic["Name"];

                PListInteger num = (PListInteger)weaponDic["Num"];
                PListInteger numIcon = (PListInteger)weaponDic["NumIcon"];

                PListString dspt = (PListString)weaponDic["Dspt"];
                PListInteger grow = (PListInteger)weaponDic["Grow"];
                PListInteger numEffect = (PListInteger)weaponDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)weaponDic["ValueEffect"];
                PListInteger price = (PListInteger)weaponDic["Price"];
                PListInteger type = (PListInteger)weaponDic["Type"];
                PListInteger valueAbility = (PListInteger)weaponDic["ValueAbility"];

                Equipment weapon = new Equipment();
                weapon.name = name.Value;
                weapon.num = num.Value;
                weapon.dspt = dspt.Value;
                weapon.numEffect = numEffect.Value;
                weapon.valueEffect = valueEffect.Value;
                weapon.numIcon = Convert.ToInt64(numIcon.Value);
                weapon.price = price.Value;

                weapon.grow = grow.Value;
                weapon.teamEnable = 0;
                weapon.type = type.Value;
                weapon.valueAbility = valueAbility.Value;
                weapons.Add(weapon);

            }
            foreach (KeyValuePair<String, IPListElement> a in armorsDic)
            {
                PListDict armorDic = (PListDict)a.Value;
                PListString name = (PListString)armorDic["Name"];

                PListInteger num = (PListInteger)armorDic["Num"];
                PListInteger numIcon = (PListInteger)armorDic["NumIcon"];

                PListString dspt = (PListString)armorDic["Dspt"];
                PListInteger grow = (PListInteger)armorDic["Grow"];
                PListInteger numEffect = (PListInteger)armorDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)armorDic["ValueEffect"];
                PListInteger price = (PListInteger)armorDic["Price"];
                PListInteger type = (PListInteger)armorDic["Type"];
                PListInteger valueAbility = (PListInteger)armorDic["ValueAbility"];

                Equipment armor = new Equipment();
                armor.name = name.Value;
                armor.num = num.Value;
                armor.dspt = dspt.Value;
                armor.numEffect = numEffect.Value;
                armor.valueEffect = valueEffect.Value;
                armor.numIcon = Convert.ToInt64(numIcon.Value);
                armor.price = price.Value;

                armor.grow = grow.Value;
                armor.teamEnable = 0;
                armor.type = type.Value;
                armor.valueAbility = valueAbility.Value;
                armors.Add(armor);
            }
            foreach (KeyValuePair<String, IPListElement> a in assistsDic)
            {
                PListDict assistDic = (PListDict)a.Value;
                PListString name = (PListString)assistDic["Name"];

                PListInteger num = (PListInteger)assistDic["Num"];
                PListInteger numIcon = (PListInteger)assistDic["NumIcon"];
                PListInteger teamEnable = (PListInteger)assistDic["TeamEnable"];
                PListString dspt = (PListString)assistDic["Dspt"];
                PListInteger numEffect = (PListInteger)assistDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)assistDic["ValueEffect"];
                PListInteger price = (PListInteger)assistDic["Price"];

                PListInteger valueAbility = (PListInteger)assistDic["ValueAbility"];

                Equipment assist = new Equipment();
                assist.name = name.Value;
                assist.num = num.Value;
                assist.dspt = dspt.Value;
                assist.numEffect = numEffect.Value;
                assist.valueEffect = valueEffect.Value;
                assist.numIcon = Convert.ToInt64(numIcon.Value);
                assist.price = price.Value;

                assist.grow = 0;


                assist.valueAbility = valueAbility.Value;
                assists.Add(assist);
            }
            foreach (KeyValuePair<String, IPListElement> a in SundrisDic)
            {
                PListDict sundryDic = (PListDict)a.Value;
                PListString name = (PListString)sundryDic["Name"];

                PListInteger num = (PListInteger)sundryDic["Num"];
                PListInteger numIcon = (PListInteger)sundryDic["NumIcon"];
                PListString dspt = (PListString)sundryDic["Dspt"];
                PListInteger numEffect = (PListInteger)sundryDic["NumEffect"];
                PListInteger valueEffect = (PListInteger)sundryDic["ValueEffect"];
                PListInteger price = (PListInteger)sundryDic["Price"];



                Equipment sundry = new Equipment();
                sundry.name = name.Value;
                sundry.num = num.Value;
                sundry.dspt = dspt.Value;
                sundry.numEffect = numEffect.Value;
                sundry.valueEffect = valueEffect.Value;
                sundry.numIcon = numIcon.Value;
                sundry.price = price.Value;

                sundry.grow = 0;
                sundry.teamEnable = 0;
                sundry.type = 0;
                sundry.valueAbility = 0;
                sundries.Add(sundry);

            }
        }
        private void LoadMagicData(String fileName_)
        {
            magics = new List<Magic>();
            PListRoot root = PListRoot.Load(fileName_);
            root = PListRoot.Load(fileName_);
            PListDict rootDic = (PListDict)root.Root;
            PListDict magicsDic = (PListDict)rootDic["Magics"];
            foreach (KeyValuePair<String, IPListElement> a in magicsDic)
            {
                PListDict magicDic = (PListDict)a.Value;
                PListString name = (PListString)magicDic["Name"];
                PListInteger num = (PListInteger)magicDic["Num"];
                PListInteger numIcon = (PListInteger)magicDic["NumIcon"];
                PListString dspt = (PListString)magicDic["Dspt"];
                PListInteger numEffect = (PListInteger)magicDic["NumEffect"];
                PListInteger type = (PListInteger)magicDic["Type"];
                PListInteger target = (PListInteger)magicDic["Target"];
                PListInteger consume = (PListInteger)magicDic["Consume"];
                PListInteger numAnimCall = (PListInteger)magicDic["NumAnimCall"];
                PListInteger numAnimMeff = (PListInteger)magicDic["NumAnimMeff"];
                PListInteger numAnimSeff = (PListInteger)magicDic["NumAnimSeff"];
                PListInteger numRangeAttack = (PListInteger)magicDic["NumRangeAttack"];
                PListInteger numRangeEffect = (PListInteger)magicDic["NumRangeEffect"];
                PListInteger valueAlpha = (PListInteger)magicDic["ValueAlpha"];
                PListInteger valueC = (PListInteger)magicDic["ValueC"];

                Magic magic = new Magic();
                magic.name = name.Value;
                magic.num = num.Value;
                magic.dspt = dspt.Value;
                magic.numEffect = numEffect.Value;
                magic.numIcon = Convert.ToInt64(numIcon.Value);
                magic.type = type.Value;
                magic.target = target.Value;
                magic.consume = consume.Value;
                magic.numAnimCall = numAnimCall.Value;
                magic.numAnimMeff = numAnimMeff.Value;
                magic.numAnimSeff = numAnimSeff.Value;
                magic.numRangeAttack = numRangeAttack.Value;
                magic.numRangeEffect = numRangeEffect.Value;
                magic.valueAlpha = valueAlpha.Value;
                magic.valueC = valueC.Value;

                magics.Add(magic);

            }

        }
        #region Interface
        private void button1_Click(object sender, EventArgs e)
        {
            if (lbFileName.Text.Contains("New"))
            {
                DialogResult result = saveFileDialog1.ShowDialog(this);
                if (result == DialogResult.OK)
                {
                    lbFileName.Text = saveFileDialog1.FileName;
                }
            }
            currentFile.Save(lbFileName.Text, PListFormat.Xml);

        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();

            DialogResult result = openFileDialog1.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                lbFileName.Text = openFileDialog1.FileName;
                currentFile = PListRoot.Load(lbFileName.Text);
                PListDict rootDic =(PListDict) currentFile.Root;
                if (rootDic.ContainsKey("PreSetting"))
                    displaySDramaTree();
                else
                    displayRDramaTree();

            }
        }

        private void buttonNewR_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            lbFileName.Text = "NewRFile.plist";
            LoadInitRDrama();
            displayRDramaTree();
        }
        private void buttonNewSDrama_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            lbFileName.Text = "NewSFile.plist";
            LoadInitSDrama();
            displaySDramaTree();
        }

        private PListArray InitSTrigger()
        {
            PListRoot root = PListRoot.Load(Form1.cczDirector + "\\DramaPrototype.plist");
            PListDict prototypeRootDic = (PListDict)((PListDict)root.Root)["Prototype"];
            PListArray trigger = new PListArray();
            PListDict section = (PListDict)prototypeRootDic["BattleWin"];
            trigger.Add(section);
            section = (PListDict)prototypeRootDic["BattleLose"];
            trigger.Add(section);
            return trigger;
        }
        private void treeView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {   //右键
                TreeNode tn = treeView1.GetNodeAt(e.X, e.Y);
                

                if (tn != null&&tn.Level!=0&&tn.Level!=1)
                {
                    contextMenuStrip1.Show(treeView1, e.X, e.Y);
                    treeView1.SelectedNode = tn;
                    if (tn.Level == 2 && tn.Nodes.Count != 0)
                    {
                        toolStripMenuItemEdit.Enabled = false;
                        toolStripMenuItemAdd.Enabled = true;
                        toolStripMenuItemDelete.Enabled = true;
                        toolStripMenuItemAddInstruct.Enabled = false;
                        toolStripMenuItemAddSubEvent.Enabled = true;
                    }else if (tn.Level == 3)
                    {
                        toolStripMenuItemEdit.Enabled = true;
                        toolStripMenuItemAdd.Enabled = false;
                        toolStripMenuItemDelete.Enabled = false;
                        toolStripMenuItemAddInstruct.Enabled = false;
                        toolStripMenuItemAddSubEvent.Enabled = false;
                    }else if (tn.Level == 4||tn.Level==5)
                    {
                        toolStripMenuItemEdit.Enabled = true;
                        toolStripMenuItemAdd.Enabled = true;
                        toolStripMenuItemDelete.Enabled = true;
                        toolStripMenuItemAddInstruct.Enabled = true;
                        toolStripMenuItemAddSubEvent.Enabled = false;
                    }
                    if (tn.Nodes.Count != 0)
                    {
                        toolStripMenuItemEdit.Enabled = false;

                    }
                    if (tn.Text == "Condition" || tn.Parent.Text == "Condition")
                    {
                        toolStripMenuItemAddTest.Enabled = true;
                        toolStripMenuItemAddInstruct.Enabled = false;
                    }
                    else
                    {
                        toolStripMenuItemAddTest.Enabled = false;
                        toolStripMenuItemAddInstruct.Enabled = true;
                    }
                    if (tn.Text == "Section")
                    {
                        toolStripMenuItemAddInstruct.Enabled = false;

                    }
                }
            }
        }
        private void toolStripMenuItemAddSubEvent_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode.Parent.Text == "Story")
            {
                PListDict rootDic = (PListDict)currentFile.Root;
                PListArray story = (PListArray)rootDic["Story"];
                PListDict scene = new PListDict();
                story.Insert(treeView1.SelectedNode.Index, scene);
                scene.Add("Information", new PListString("内部信息"));
                PListArray dramas = new PListArray();
                dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaBegin));
                dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaOver));
                scene.Add("Dramas", dramas);

                TreeNode sceneNode = new TreeNode("Scene");
                treeView1.SelectedNode.Parent.Nodes.Insert(treeView1.SelectedNode.Index, sceneNode);
                TreeNode info = new TreeNode("内部信息:" + ((PListString)scene["Information"]).Value);
                sceneNode.Nodes.Add(info);
                TreeNode dramasNode = new TreeNode("Dramas");
                sceneNode.Nodes.Add(dramasNode);
                PListArray dramaArr = (PListArray)scene["Dramas"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    long type = ((PListInteger)instrctDic["InstructType"]).Value;
                    TreeNode instructNode = new TreeNode(" " + Constant.instructTypeDic[(int)type]);
                    dramasNode.Nodes.Add(instructNode);
                }
            }
            else if (treeView1.SelectedNode.Parent.Text == "Council")
            {
                PListDict rootDic = (PListDict)currentFile.Root;
                PListArray council = (PListArray)rootDic["Council"];
                PListDict section = new PListDict();
                section.Add("Information", new PListString("内部信息"));
                section.Add("HeroID", new PListInteger(0));
                PListArray dramas = new PListArray();
                dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaBegin));
                dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaOver));
                section.Add("Dramas", dramas);
                council.Insert(treeView1.SelectedNode.Index,section);

                TreeNode sectionNode = new TreeNode("Section");
                treeView1.SelectedNode.Parent.Nodes.Insert(treeView1.SelectedNode.Index, sectionNode);
                TreeNode info = new TreeNode("内部信息:" + ((PListString)section["Information"]).Value);
                sectionNode.Nodes.Add(info);
                TreeNode heroNode = new TreeNode("点击武将：" + GetHeroName(((PListInteger)section["HeroID"]).Value));
                sectionNode.Nodes.Add(heroNode);
                TreeNode dramasNode = new TreeNode("Dramas");
                sectionNode.Nodes.Add(dramasNode);
                PListArray dramaArr = (PListArray)section["Dramas"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    long type = ((PListInteger)instrctDic["InstructType"]).Value;
                    TreeNode instructNode = new TreeNode(" " + type);
                    dramasNode.Nodes.Add(instructNode);
                }
            }
            else if (treeView1.SelectedNode.Parent.Text == "Trigger")
            {

                PListDict rootDic = (PListDict)currentFile.Root;
                PListArray trigger = (PListArray)rootDic["Trigger"];
                PListDict section = new PListDict();
                section.Add("Information", new PListString("内部信息"));
                PListArray conditions = new PListArray();
                conditions.Add(AddDramaDialog.CreateConditionDict(kLogicalTestType.kLogicalTestTypeVariable));
                section.Add("Condition", conditions);
                PListArray dramas = new PListArray();
                dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaBegin));
                dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaOver));
                section.Add("Dramas", dramas);
                trigger.Insert(treeView1.SelectedNode.Index, section);

                TreeNode sectionNode = new TreeNode("Section");
                treeView1.SelectedNode.Parent.Nodes.Insert(treeView1.SelectedNode.Index, sectionNode);
                TreeNode info = new TreeNode("内部信息:" + ((PListString)section["Information"]).Value);
                sectionNode.Nodes.Add(info);
                TreeNode conditionNode = new TreeNode("Condition");
                sectionNode.Nodes.Add(conditionNode);
                PListArray conditionArr = (PListArray)section["Condition"];
                foreach (PListDict instrctDic in conditionArr)
                {
                    long type = ((PListInteger)instrctDic["TestType"]).Value;

                    TreeNode instructNode = new TreeNode(" " + Constant.logicalTestType[(int)type]);
                    conditionNode.Nodes.Add(instructNode);
                }
                TreeNode dramasNode = new TreeNode("Dramas");
                sectionNode.Nodes.Add(dramasNode);
                PListArray dramaArr = (PListArray)section["Dramas"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    long type = ((PListInteger)instrctDic["InstructType"]).Value;

                    TreeNode instructNode = new TreeNode(" " + Constant.instructTypeDic[(int)type]);
                    dramasNode.Nodes.Add(instructNode);
                }
            }
        }
        private void toolStripMenuItemAddInstruct_Click(object sender, EventArgs e)
        {
            addDramaDialog.Show(this, false);

        }
        private void 添加测试条件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addDramaDialog.Show(this, true);

        }

        private void toolStripMenuItemDelete_Click(object sender, EventArgs e)
        {
            PListDict dict = (PListDict)GetPlistElement(treeView1.SelectedNode);
            PListArray parentDict = (PListArray)GetPlistElement(treeView1.SelectedNode.Parent);
            parentDict.Remove(dict);
            treeView1.SelectedNode.Remove();
        }

        private void toolStripMenuItemEdit_Click(object sender, EventArgs e)
        {

            ShowEditDialog();
            
        }
        #endregion
        #region Callback
        public void addDramaCode(PListDict instruct,TreeNode node)
        {
            PListArray dramasPList = (PListArray)GetPlistElement(treeView1.SelectedNode.Parent);
            dramasPList.Insert( treeView1.SelectedNode.Index,instruct);
            treeView1.SelectedNode.Parent.Nodes.Insert(treeView1.SelectedNode.Index, node);
        }
        public void addSectionCode(PListDict instruct, TreeNode node)
        {
            PListDict rootDic = (PListDict)currentFile.Root;
            PListArray trigger = (PListArray)rootDic["Trigger"];
            trigger.Insert(treeView1.SelectedNode.Index, instruct);
            treeView1.SelectedNode.Parent.Nodes.Insert(treeView1.SelectedNode.Index, node);
        }
        public void editInformation(string result)
        {
            treeView1.SelectedNode.Text = "内部信息：" + result;
            PListString elem = (PListString)GetPlistElement(treeView1.SelectedNode);
            elem.Value = result;
        }
        public void editNormalInstruct(PListDict newDict)
        {
            PListDict oldDict = (PListDict)GetPlistElement(treeView1.SelectedNode);
            for (int i = 0; i != newDict.Count; ++i)
            {
                string key = newDict.Keys.ElementAt<string>(i);
                oldDict[key] = newDict[key];
            }
        }
        public void editHeroTouched(string name)
        {
            Hero hero = heros.Find((h) => { return h.name == name; });
            treeView1.SelectedNode.Text = "点击武将：" + hero.name;
            PListInteger elem = (PListInteger)GetPlistElement(treeView1.SelectedNode);
            elem.Value = (long)hero.num;
        }
        #endregion
        #region Helper
        private void ShowEditDialog()
        {
            if ((treeView1.SelectedNode.Level == 3 && treeView1.SelectedNode.Index == 0) || treeView1.SelectedNode.Text.Contains("内部信息"))
                editInfoDialog.ShowDialog(this);
            else if (treeView1.SelectedNode.Level == 3 && treeView1.SelectedNode.Index == 1)
            {
                editHeroDialog.ShowDialog(this);
            }
            else if (treeView1.SelectedNode.Parent.Text == "Condition")
            {
                PListDict dict = (PListDict)GetPlistElement(treeView1.SelectedNode);
                kLogicalTestType testType = (kLogicalTestType)((PListInteger)dict["TestType"]).Value;
                editDramaDialog.Show(this, dict, treeView1.SelectedNode);
                TreeNode node = AddDramaDialog.CreateConditionTreeNode(dict);
                treeView1.SelectedNode.Text = node.Text;
            }
            else
            {
                PListDict dict = (PListDict)GetPlistElement(treeView1.SelectedNode);
                kInstructType dictInstructType = (kInstructType)((PListInteger)dict["InstructType"]).Value;
                if (dictInstructType == kInstructType.kInstructTypeChangeAI)
                {
                    editDramaChangeAIDialog.Show(dict, this);
                }
                else if (dictInstructType == kInstructType.kInstructTypeSetEquipment)
                {
                    editDramaSetEquipDialog.Show(this, dict);
                }
                else if (dictInstructType == kInstructType.kInstructTypeSetPlayerBattle)
                {
                    editDramaSetPlayerBattleDialog.Show(this, dict);
                }
                else if (dictInstructType == kInstructType.kInstructTypeSetEnemyBattle
                    || dictInstructType == kInstructType.kInstructTypeSetFriendBattle)
                {
                    editDramaSetAIBattleDialog.Show(this, dict);
                }
                else if (dictInstructType == kInstructType.kInstructTypeBattleSetState)
                {
                    editDramaSetStateDialog.Show(this, dict);
                }
                else if (dictInstructType == kInstructType.kInstructTypeBattleExit 
                    || dictInstructType == kInstructType.kInstructTypeBattleHideUnit)
                {
                    editDramaSetExitUnitDialog.Show(this, dict);
                }
                else if (dictInstructType == kInstructType.kInstructTypeBattleDisplayWin)
                {
                    editDramaSetWinRewardDialog.Show(this, dict);
                }
                else
                    editDramaDialog.Show(this, dict, treeView1.SelectedNode);
                TreeNode node = AddDramaDialog.CreateTreeNode(dict);
                if(node.Nodes.Count==0)
                    treeView1.SelectedNode.Text = node.Text;
            }
        }
        static public PListDict GetInstructDict(kInstructType type)
        {
            PListDict dict = new PListDict();
            if (type == kInstructType.kInstructTypeDramaBegin)
            {
                dict.Add("InstructType", new PListInteger((long)type));
            }
            else if (type == kInstructType.kInstructTypeDramaOver)
            {
                dict.Add("InstructType", new PListInteger((long)type));
            }
            return dict;
        }
        private String GetHeroName(long heroID)
        {
            Hero hero = heros.Find((h) => { return h.num == heroID; });
            return hero.name;
        }
        private void LoadInitSDrama()
        {
            currentFile = new PListRoot();
            PListDict rootDic = new PListDict();
            currentFile.Root = rootDic;
            //Story
            PListArray story = new PListArray();
            rootDic.Add("Story", story);
            PListRoot root = PListRoot.Load(Form1.cczDirector + "\\DramaPrototype.plist");
            PListDict prototypeRootDic = (PListDict)((PListDict)root.Root)["Prototype"];
            PListDict scene = (PListDict)prototypeRootDic["SStory"];
            story.Add(scene);

            //Trigger
            rootDic.Add("Trigger", InitSTrigger());
            //PreSetting
            PListArray preSetting = new PListArray();
            rootDic.Add("PreSetting", preSetting);
            preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetPlaceName));
            preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetGlobalVar));
            preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetPlayerBattle));
            preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetEnemyBattle));
            preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetFriendBattle));
            //LastSetting
            PListDict lastSetting = new PListDict();
            rootDic.Add("LastSetting", lastSetting);
            lastSetting.Add("Information", new PListString("战后处置"));
            PListArray dramas = new PListArray();
            dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaBegin));
            dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaOver));
            lastSetting.Add("Dramas", dramas);
            

        }
        private void LoadInitRDrama(){
            currentFile = new PListRoot();
            PListDict rootDic = new PListDict();
            currentFile.Root = rootDic;
            //Story
            PListArray story = new PListArray();
            rootDic.Add("Story", story);
            PListDict scene = new PListDict();
            story.Add(scene);
            scene.Add("Information", new PListString("内部信息"));
            PListArray dramas = new PListArray();
            dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaBegin));
            dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaOver));
            scene.Add("Dramas", dramas);
            //Council
            PListArray council = new PListArray();
            rootDic.Add("Council", council);
            PListDict section = new PListDict();
            section.Add("Information", new PListString("内部信息"));
            section.Add("HeroID", new PListInteger(0));
            dramas = new PListArray();
            dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaBegin));
            dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaOver));
            section.Add("Dramas", dramas);
            council.Add(section);
            //SpecialStory
            story = new PListArray();
            rootDic.Add("SpecialStory", story);
            scene = new PListDict();
            story.Add(scene);
            scene.Add("Information", new PListString("内部信息"));
            dramas = new PListArray();
            dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaBegin));
            dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaOver));
            scene.Add("Dramas", dramas);
        }
        private void displayRDramaTree()
        {
            TreeNode rootNode = new TreeNode();
            rootNode.Text = "Root";
            treeView1.Nodes.Add(rootNode);
            TreeNode storyNode = new TreeNode("Story");
            TreeNode specialStoryNode = new TreeNode("SpecialStory");

            TreeNode councilNode = new TreeNode("Council");
            rootNode.Nodes.Add(storyNode);
            rootNode.Nodes.Add(councilNode);
            rootNode.Nodes.Add(specialStoryNode);

            PListDict rootDic = (PListDict)currentFile.Root;
            PListArray story = (PListArray)rootDic["Story"];
            foreach (PListDict scene in story)
            {
                TreeNode sceneNode = new TreeNode("Scene");
                storyNode.Nodes.Add(sceneNode);
                TreeNode info = new TreeNode("内部信息:" + ((PListString)scene["Information"]).Value);
                sceneNode.Nodes.Add(info);
                TreeNode dramasNode = new TreeNode("Dramas");
                sceneNode.Nodes.Add(dramasNode);
                PListArray dramaArr = (PListArray)scene["Dramas"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = AddDramaDialog.CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
            }
            story = (PListArray)rootDic["SpecialStory"];
            foreach (PListDict scene in story)
            {
                TreeNode sceneNode = new TreeNode("Scene");
                specialStoryNode.Nodes.Add(sceneNode);
                TreeNode info = new TreeNode("内部信息:" + ((PListString)scene["Information"]).Value);
                sceneNode.Nodes.Add(info);
                TreeNode dramasNode = new TreeNode("Dramas");
                sceneNode.Nodes.Add(dramasNode);
                PListArray dramaArr = (PListArray)scene["Dramas"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = AddDramaDialog.CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
            }
            PListArray council = (PListArray)rootDic["Council"];
            foreach (PListDict section in council)
            {
                TreeNode sectionNode = new TreeNode("Section");
                councilNode.Nodes.Add(sectionNode);
                TreeNode info = new TreeNode("内部信息:" + ((PListString)section["Information"]).Value);
                sectionNode.Nodes.Add(info);
                TreeNode heroNode = new TreeNode("点击武将：" + GetHeroName(((PListInteger)section["HeroID"]).Value));
                sectionNode.Nodes.Add(heroNode);
                TreeNode dramasNode = new TreeNode("Dramas");
                sectionNode.Nodes.Add(dramasNode);
                PListArray dramaArr = (PListArray)section["Dramas"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = AddDramaDialog.CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
            }


        }
        private void displaySDramaTree()
        {
            TreeNode rootNode = new TreeNode();
            rootNode.Text = "Root";
            treeView1.Nodes.Add(rootNode);
            TreeNode storyNode = new TreeNode("Story");
            TreeNode triggerNode = new TreeNode("Trigger");
            TreeNode preSettingNode = new TreeNode("PreSetting");
            rootNode.Nodes.Add(storyNode);
            rootNode.Nodes.Add(triggerNode);
            rootNode.Nodes.Add(preSettingNode);
            //Story
            PListDict rootDic = (PListDict)currentFile.Root;
            PListArray story = (PListArray)rootDic["Story"];
            foreach (PListDict scene in story)
            {
                TreeNode sceneNode = new TreeNode("Scene");
                storyNode.Nodes.Add(sceneNode);
                TreeNode info = new TreeNode("内部信息:" + ((PListString)scene["Information"]).Value);
                sceneNode.Nodes.Add(info);
                TreeNode dramasNode = new TreeNode("Dramas");
                sceneNode.Nodes.Add(dramasNode);
                PListArray dramaArr = (PListArray)scene["Dramas"];
                foreach (PListDict instrctDic in dramaArr)
                {
                    TreeNode instructNode = AddDramaDialog.CreateTreeNode(instrctDic);
                    dramasNode.Nodes.Add(instructNode);
                }
            }
            //Trigger
            PListArray trigger = (PListArray)rootDic["Trigger"];
            foreach (PListDict section in trigger)
            {
                TreeNode sectionNode = CreateTriggerSectionNode(section);
                triggerNode.Nodes.Add(sectionNode);
                
            }
            //PreSetting
            PListArray preSetting = (PListArray)rootDic["PreSetting"];

            foreach (PListDict instrctDic in preSetting)
            {
                long type = ((PListInteger)instrctDic["InstructType"]).Value;

                TreeNode instructNode = new TreeNode(" " + Constant.instructTypeDic[(int)type]);
                preSettingNode.Nodes.Add(instructNode);
            }
            //LastSetting
            PListDict lastSetting = (PListDict)rootDic["LastSetting"];
            TreeNode lastSettingNode = new TreeNode("LastSetting");
            rootNode.Nodes.Add(lastSettingNode);
            TreeNode lastSettingInfo = new TreeNode("内部信息:" + ((PListString)lastSetting["Information"]).Value);
            lastSettingNode.Nodes.Add(lastSettingInfo);
            TreeNode dramasNode_ = new TreeNode("Dramas");
            lastSettingNode.Nodes.Add(dramasNode_);
            PListArray dramaArr_ = (PListArray)lastSetting["Dramas"];
            foreach (PListDict instrctDic in dramaArr_)
            {
                TreeNode instructNode = AddDramaDialog.CreateTreeNode(instrctDic);
                dramasNode_.Nodes.Add(instructNode);
            }
        }
        public TreeNode CreateTriggerSectionNode(PListDict section)
        {
            TreeNode sectionNode = new TreeNode("Section");
            TreeNode info = new TreeNode("内部信息:" + ((PListString)section["Information"]).Value);
            sectionNode.Nodes.Add(info);
            TreeNode conditionNode = new TreeNode("Condition");
            sectionNode.Nodes.Add(conditionNode);
            PListArray conditionArr = (PListArray)section["Condition"];
            foreach (PListDict instrctDic in conditionArr)
            {
                long type = ((PListInteger)instrctDic["TestType"]).Value;

                TreeNode instructNode = AddDramaDialog.CreateConditionTreeNode(instrctDic);
                conditionNode.Nodes.Add(instructNode);
            }
            TreeNode dramasNode = new TreeNode("Dramas");
            sectionNode.Nodes.Add(dramasNode);
            PListArray dramaArr = (PListArray)section["Dramas"];
            foreach (PListDict instrctDic in dramaArr)
            {
                long type = ((PListInteger)instrctDic["InstructType"]).Value;

                TreeNode instructNode = AddDramaDialog.CreateTreeNode(instrctDic);
                dramasNode.Nodes.Add(instructNode);
            }
            return sectionNode;
        }
        private IPListElement GetPlistElement(TreeNode node)
        {
            PListDict rootDic = (PListDict)currentFile.Root;
            if (node.Level == 0)
            {
                return rootDic;
            }
            else if (node.Level == 1)
            {
                return rootDic[node.Text];
            }
            else if (node.Text == "条件")
            {
                return GetPlistElement(node.Parent);
            }
            IPListElement parentElem = GetPlistElement(node.Parent);
            if (parentElem is PListDict)
            {
                PListDict item = (PListDict)parentElem;
                if (item.ContainsKey("InstructType"))
                {
                    //指令结点
                    PListInteger instructType = (PListInteger)item["InstructType"];
                    if (instructType.Value == (long)kInstructType.kInstructTypeConditionTest&&node.Index==0)
                    {
                        return item["Information"]; ;
                    }
                    return item[node.Text];
                }
                else
                {
                    //结构节点
                    if (node.Index == 0)
                    {
                        return item["Information"];
                    }
                    else if (node.Index == 1 && node.NextNode != null)
                    {
                        if (item.ContainsKey("HeroID"))
                            return item["HeroID"];
                        else if (item.ContainsKey("Condition"))
                            return item["Condition"];
                        else
                            return null;
                    }
                    else
                    {
                        return item["Dramas"];
                    }
                }
            }
            else if (parentElem is PListArray)
            {
                return ((PListArray)parentElem)[node.Index];
            }
            else
            {
                return null;
            }
        }

        #endregion

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {   //左键双击键
                TreeNode tn = treeView1.GetNodeAt(e.X, e.Y);
                if (tn != null && tn.Level >= 2  && tn.Nodes.Count == 0)
                {
                    ShowEditDialog();

                }
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonDefaultSort.Checked)
            {
                heros.Sort(new HeroNumCompare());
            }
        }

        private void radioButtonNameSort_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonNameSort.Checked)
            {
                heros.Sort(new HeroNameCompare());
            }
        }
        public void copySelectedInstructDict()
        {
            if (treeView1.SelectedNode == null )
            {
                return;
            }

            if (treeView1.SelectedNode.Parent.Text == "Dramas"
                || treeView1.SelectedNode.Parent.Text.Contains("SubDrama")
                || treeView1.SelectedNode.Parent.Text == "Condition" 
                || treeView1.SelectedNode.Parent.Text == "Trigger")
            {
                PListDict oldDict = (PListDict)GetPlistElement(treeView1.SelectedNode);
                PListRoot root = new PListRoot();
                root.Root = oldDict;
                root.Save(Form1.cczDirector + "/CopyTemp.plist");
            }
        }
        public void pasteSelectedInstuctDict()
        {
            if ((treeView1.SelectedNode == null))
                return;
            PListRoot root = PListRoot.Load(Form1.cczDirector + "/CopyTemp.plist");
            PListDict cloneDict = (PListDict)root.Root;
            if (treeView1.SelectedNode.Parent.Text == "Dramas" 
                || treeView1.SelectedNode.Parent.Text.Contains("SubDrama"))
            {
                TreeNode node = AddDramaDialog.CreateTreeNode(cloneDict);
                addDramaCode(cloneDict, node);
            }
            else if (treeView1.SelectedNode.Parent.Text == "Condition")
            {
                TreeNode node = AddDramaDialog.CreateConditionTreeNode(cloneDict);
                addDramaCode(cloneDict, node);
            }
            else if (treeView1.SelectedNode.Parent.Text == "Trigger")
            {
                
                TreeNode node = CreateTriggerSectionNode(cloneDict); ;
                addSectionCode(cloneDict, node);
            }
        }
        private void 复制ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            copySelectedInstructDict();

        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pasteSelectedInstuctDict();
        }

        private void 添加命令组合ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addcommonInstructDialog.Show(this);
        }

        private void buttonConvert_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            DialogResult result = openFileDialog2.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                if (openFileDialog2.FileName.Contains("R_"))
                {
                    currentFile = new PListRoot();
                    PListDict rootDic = new PListDict();
                    currentFile.Root = rootDic;
                    //Story
                    PListArray story = new PListArray();
                    rootDic.Add("Story", story);
                    //SpecialStory
                    story = new PListArray();
                    rootDic.Add("SpecialStory", story);
                    //Council
                    PListArray council = new PListArray();
                    rootDic.Add("Council", council);
                }
                else
                {
                    currentFile = new PListRoot();
                    PListDict rootDic = new PListDict();
                    currentFile.Root = rootDic;
                    //Story
                    PListArray story = new PListArray();
                    rootDic.Add("Story", story);
                    //Trigger
                    PListArray trigger = new PListArray();
                    rootDic.Add("Trigger", trigger);
                    //PreSetting
                    PListArray preSetting = new PListArray();
                    rootDic.Add("PreSetting", preSetting);
                    preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetPlaceName));
                    preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetGlobalVar));
                    preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetPlayerBattle));
                    preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetEnemyBattle));
                    preSetting.Add(AddDramaDialog.CreateInstructDict(kInstructType.kInstructTypeSetFriendBattle));
                    //LastSetting
                    PListDict lastSetting = new PListDict();
                    rootDic.Add("LastSetting", lastSetting);
                    lastSetting.Add("Information", new PListString("战后处置"));
                    PListArray dramas = new PListArray();
                    dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaBegin));
                    dramas.Add(GetInstructDict(kInstructType.kInstructTypeDramaOver));
                    lastSetting.Add("Dramas", dramas);
                }
                dramaFileData.LoadData(openFileDialog2.FileName,(PListDict)currentFile.Root);
                if (openFileDialog2.FileName.Contains("R_"))
                {
                    displayRDramaTree();
                }
                else
                {
                    displaySDramaTree();
                }
            }
            
        }

        private void buttonMassConvet_Click(object sender, EventArgs e)
        {
            dramaConvertDialog.Show(currentFile, this,this);
        }







    }
}
